<!--
 * Description: 同步数据页面
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Qiuxue.Wu
 * Date: 2020/07/14 15:16
-->

<template>
  <BaseWrap v-loading="isRequesting">
    <div style="padding:0px 10px">
      <el-collapse v-model="activeName" accordion>
        <el-collapse-item name="1" :title="$t('Sync.title1')">
          <div class="custom-item-content">
            <div class="top">
              <el-button
                v-if="$hasPer('syncData')"
                class="btn-custom"
                icon="el-icon-refresh"
                size="small"
                type="warning"
                @click="syncSapPurchasePrice"
              >
                {{ $t('button.syncData') }}
              </el-button>
              <el-button
                class="btn-custom"
                icon="el-icon-delete"
                size="small"
                type="danger"
                @click="truncate(1)"
              >
                清空数据
              </el-button>
            </div>
            <div class="bottom">
              <el-alert
                :closable="false"
                show-icon
                :title="`同步了 ${sapPurchasePriceData} 条数据`"
                type="success"
              ></el-alert>
            </div>
          </div>
        </el-collapse-item>

        <el-collapse-item name="2" :title="$t('Sync.title2')">
          <div class="custom-item-content">
            <div class="top">
              <el-button
                v-if="$hasPer('syncData')"
                class="btn-custom"
                icon="el-icon-refresh"
                size="small"
                type="warning"
                @click="syncSapStandardHisPrice"
              >
                {{ $t('button.syncData') }}
              </el-button>
              <el-button
                class="btn-custom"
                icon="el-icon-delete"
                size="small"
                type="danger"
                @click="truncate(2)"
              >
                清空数据
              </el-button>
            </div>
            <div class="bottom">
              <el-alert
                :closable="false"
                show-icon
                :title="`同步了 ${sapStandardHisPriceData} 条数据`"
                type="success"
              ></el-alert>
            </div>
          </div>
        </el-collapse-item>
        <el-collapse-item name="3" :title="$t('Sync.title3')">
          <div class="custom-item-content">
            <div class="top">
              <el-button
                v-if="$hasPer('syncData')"
                class="btn-custom"
                icon="el-icon-refresh"
                size="small"
                type="warning"
                @click="syncSapStandardPrice"
              >
                {{ $t('button.syncData') }}
              </el-button>
              <el-button
                class="btn-custom"
                icon="el-icon-delete"
                size="small"
                type="danger"
                @click="truncate(0)"
              >
                清空数据
              </el-button>
            </div>
            <div class="bottom">
              <el-alert
                :closable="false"
                show-icon
                :title="`同步了 ${sapStandardPriceData} 条数据`"
                type="success"
              ></el-alert>
            </div>
          </div>
        </el-collapse-item>
        <!-- FD物料号和临时物料号映射关系 -->
        <el-collapse-item name="4" title="FD物料号和临时物料号映射关系">
          <div class="custom-item-content">
            <div class="top">
              <el-button
                v-if="$hasPer('syncData')"
                class="btn-custom"
                icon="el-icon-refresh"
                size="small"
                type="warning"
                @click="manualSyncFdPnAndTempPn"
              >
                {{ $t('button.syncData') }}
              </el-button>
            </div>
            <div class="bottom">
              <el-alert
                :closable="false"
                show-icon
                :title="`同步了 ${fdPnAndTempPnData} 条数据`"
                type="success"
              ></el-alert>
            </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </BaseWrap>
</template>
<script>
import {
  syncSapPurchasePrice,
  syncSapStandardHisPrice,
  syncSapStandardPrice,
  scheduleClearSapPrice,
  syncFdPnAndTempPn
} from '@/api/schedule/schedule'
export default {
  name: 'SyncData',
  data() {
    return {
      isRequesting: false,
      activeName: '1',
      sapPurchasePriceData: 0,
      sapStandardHisPriceData: 0,
      sapStandardPriceData: 0,
      fdPnAndTempPnData: 0
    }
  },
  methods: {
    /** 同步SAP系统采购价数据 */
    async syncSapPurchasePrice() {
      try {
        this.isRequesting = true
        const res = await syncSapPurchasePrice({}, { msgSuccess: true, msgError: true })
        this.sapPurchasePriceData = res.data
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },

    /** 同步SAP系统历史标准价数据 */
    async syncSapStandardHisPrice() {
      try {
        this.isRequesting = true
        const res = await syncSapStandardHisPrice({}, { msgSuccess: true, msgError: true })
        this.sapStandardHisPriceData = res.data
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },

    /** 同步SAP系统标准价数据 */
    async syncSapStandardPrice() {
      try {
        this.isRequesting = true
        const res = await syncSapStandardPrice({}, { msgSuccess: true, msgError: true })
        this.sapStandardPriceData = res.data
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },

    /** 清空SAP系统采购价数据
     *  0: 清除sap标准价信息
     *  1：清除sap采购价信息
     *  2：清除sap标准价历史信息
     */

    async truncate(type) {
      try {
        this.isRequesting = true
        const res = await scheduleClearSapPrice(
          {
            type: type
          },
          { msgSuccess: true, msgError: true }
        )
        this.sapPurchasePriceData = res.data
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },
    /**
     * 刷新FD物料号和临时物料号映射关系
     */
    async manualSyncFdPnAndTempPn() {
      try {
        this.isRequesting = true
        const { data: res } = await syncFdPnAndTempPn({}, { msgSuccess: true, msgError: true })
        if (res !== null) {
          this.fdPnAndTempPnData = res
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/ .el-collapse-item__header {
  font-weight: 700;
  font-size: 16px;
}

.custom-item-content {
  text-align: left;
  min-height: 100px;
  .bottom {
    margin-top: 20px;
    width: 30%;
  }
}
</style>
